<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>抓取简讯-后台管理-{$site.SITE_INFO.name}</title>
        <php>
            $addCss="";
            $addJs="";
            $currentNav ='简讯管理 > 抓取简讯';
        </php>
        <include file="Common:intoHead" />
    </head>
    <body>
        <div class="wrap">
            <include file="Common:head" />
            <div class="mainBody">
                <include file="Common:left" />
                <div id="Right">
                    <div class="Item hr">
                        <div class="current">抓取简讯</div>
                    </div>
                    <form>
                        <table width="100%" border="0" cellspacing="0" cellpadding="0" class="table1">
                            <tr>
                                <th width="100">抓取地址：</th>
                                <td><input id="url" type="text" class="input" size="60" name="url" /> <a href="javascript:void(0)" <if condition="$type neq 'Batch'">id="urlGetInfo"<else/>id="urlListInfo"</if>>地址信息抓取</a><img src=""> </td>
                            </tr>
                            <if condition="$type neq 'Batch'">
                                <tr>
                                    <th width="100">简讯标题：</th>
                                    <td><input id="title" type="text" class="input" size="60" name="info[title]" value="{$info.title}"/> <a href="javascript:void(0)" id="checkNewslettersTitle">检测是否重复</a></td>
                                </tr>
                                <tr>
                                    <th width="100">来源名称：</th>
                                    <td><input id="source_name" type="text" class="input" size="60" name="info[source_name]" value="{$info.source_name}"/></td>
                                </tr>
                                <tr>
                                    <th width="100">来源地址：</th>
                                    <td><input id="source_url" type="text" class="input" size="60" name="info[source_url]" value="{$info.source_url}"/></td>
                                </tr>
                                <tr>
                                    <th width="100">文章发布状态：</th>
                                    <td><label><input type="radio" name="info[status]" value="0" <if condition="$info.status eq 0"> checked="checked"</if> /> 文章审核状态</label> &nbsp; <label><input type="radio" name="info[status]" value="1" <if condition="$info.status eq 1"> checked="checked"</if> /> 文章已发布状态</label> </td>
                                </tr>
                                <tr>
                                    <th>文章摘要：</th>
                                    <td><textarea class="input" style="height: 60px; width: 600px;" name="info[summary]">{$info.summary}</textarea> 输入规则为内容总结3点，格式为用空格分隔</td>
                                </tr>
                                <tr>
                                    <th>文章内容：</th>
                                    <td><textarea id="content" class="input" style="height: 300px; width: 100%;" name="info[source_content]">{$info.source_content}</textarea></td>
                                </tr>
                            </if>
                        </table>
                        <input type="hidden" name="info[id]" value="{$info.id}" />
                        <input type="hidden" name="info[spiderType]" value="" />
                    </form>
                    <div class="commonBtnArea" >
                        <button class="btn submit"> <if condition="$type neq 'Batch'">提交<else/>批量入库</if></button>
                    </div>
                </div>
            </div>
        </div>
        <div class="clear"></div>
        <include file="Common:foot" />
        <js href="__PUBLIC__/kindeditor/kindeditor.js,__PUBLIC__/kindeditor/lang/zh_CN.js"/>
        <script type="text/javascript">
            $(function(){
                var  content ;
                KindEditor.ready(function(K) {
                    content = K.create('#content');
                });
                $("#urlGetInfo").click(function(){
                    var tmp = '';
                    var url_tmp= new Array();//临时变量，保存分割字符串
                    url_tmp=$("#url").val().split("/");//按照"/"分割
                    var p_tmp = url_tmp[url_tmp.length-1];//获取最后一部分，即文件名和参数
                    var p_id =p_tmp.split(".");//把参数和文件名分割开

                    $.getJSON("__URL__/spider{$type}/p_id/"+p_id[0], {}, function(json){
                        $("#urlGetInfo").css("color",json.status==1?"#0f0":"#f00").html('请仔细检查抓取信息结果');

                        if(json.info != null){
                            $.each(json.info, function(i,val){

                                if(i == 'content'){
                                    content.insertHtml(val);
                                }else{
                                    $("#"+i).val(val);
                                }
                            });
                        }
                    });
                });

                var list_num = 0;
                $('#urlListInfo').click(function(){
                    list_num = 0;
                    var url_tmp= parseURL($("#url").val());

                    if(url_tmp){
                        pollSpider(url_tmp['file'],parseInt(url_tmp['params']['page']));
                    }
                });

                function pollSpider(type,page){
                    $.getJSON("__URL__/spider{$type}/type/"+type+"/page/"+page, {}, function(json){
                        if(json.info != null){
                            $.each(json.info, function(i,val){
                                var str = '<tr href_url = "'+val['href']+'" source="'+val['source']+'">'+
                                        '<th width="100"><a href="javascript:void(0);" class="_spiderNew">第'+page+'页->'+(i+1)+'：</a></th>'+
                                        '<td>'+(val['source'] ? val['source'] : val['href'])+'</td>'+
                                        '</tr>';
                                $('table.table1 tbody').append(str);

                                str = '';
                                list_num++;
                            });

                            if(parseInt(page - 1) > 0){
                                pollSpider(type,(parseInt(page) - 1));
                            }else{
                                $('#urlListInfo').parent().append('<font style="color:blue">&nbsp;&nbsp;&nbsp;&nbsp;总共：'+list_num+"条数据！</font>");
                            }
                        }
                    });
                }

                //验证新闻标题
                $("#checkNewsTitle").click(function(){
                    $.getJSON("__URL__/checkNewsTitle", { title:$("#title").val(),id:"{$info.id}"}, function(json){
                        $("#checkNewsTitle").css("color",json.status==1?"#0f0":"#f00").html(json.info);
                    });
                });

                $(".submit").click(function(){
                    pollAddInfo($('tr').eq(1));
                    return false;
                });

                //tr 数据抓取
                var top_num = 100;
                function pollAddInfo(dom){
                    var _self = dom;

                    if(_self.attr('href_url') == undefined){
                        return false;
                    }

                    var url_tmp= new Array();//临时变量，保存分割字符串
                    url_tmp=_self.attr('href_url').split("/");//按照"/"分割
                    var p_tmp = url_tmp[url_tmp.length-1];//获取最后一部分，即文件名和参数
                    var p_id =p_tmp.split(".");//把参数和文件名分割开

                    $.getJSON("__URL__/addNew", { id:p_id[0],source:_self.attr('source')}, function(json){
                        _self.css("color",json.status==1?"blue":"red");

                        $("#Right").animate({scrollTop:top_num},1000);

                        top_num +=_self.height();

                        if(_self.next().first()){
                            pollAddInfo(_self.next().first());
                        }
                    });
                }
            });

            function parseURL(url) {
                var a = document.createElement('a');
                a.href = url;
                return {
                    source: url,
                    protocol: a.protocol.replace(':', ''),
                    host: a.hostname,
                    port: a.port,
                    query: a.search,
                    params: (function () {
                        var ret = {},
                                seg = a.search.replace(/^\?/, '').split('&'),
                                len = seg.length, i = 0, s;
                        for (; i < len; i++) {
                            if (!seg[i]) {
                                continue;
                            }
                            s = seg[i].split('=');
                            ret[s[0]] = s[1];
                        }
                        return ret;
                    })(),
                    file: (a.pathname.match(/\/([^\/?#]+)$/i) || [, ''])[1],
                    hash: a.hash.replace('#', ''),
                    path: a.pathname.replace(/^([^\/])/, '/$1'),
                    relative: (a.href.match(/tps?:\/\/[^\/]+(.+)/) || [, ''])[1],
                    segments: a.pathname.replace(/^\//, '').split('/')
                };
            }
        </script>
    </body>
</html>
